﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EShop.BussinessObjects;
using EShop.DataAccess;
using EShop.Common;

namespace EShop.BusinessLogic
{
    public class CBLUser
    {
        public static Boolean Update(User user, out string errMsg)
        {
            Crypto crypto = new Crypto();
            user.Password = crypto.Encrypt(user.Password);
            return CDAUser.Update(user, out errMsg);
        }

        public static Boolean Insert(User user, out string errMsg)
        {
            Crypto crypto = new Crypto();
            user.Password = crypto.Encrypt(user.Password);
            return CDAUser.Insert(user, out errMsg);
        }

        public static User Login(string LoginID, string Password)
        {
            User user_Filter = new User();
            user_Filter.ID = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective);
            user_Filter.Contact = new Contact();
            user_Filter.Contact.ID = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective);
            user_Filter.LoginID = LoginID;
            List<User> userList = CBLUser.Select(user_Filter);

            User _user = null;
            if (userList.Count > 0)
            {
                if (userList.First().Password == Password)
                    _user = userList.First();
            }

            return _user;
        }

        public static List<User> Select(User user)
        {
            List<User> userList = CDAUser.Select(user);
            foreach (User _user in userList)
            {
                Crypto crypto = new Crypto();
                _user.Password = crypto.Decrypt(userList.First().Password);
            }
            return userList;
        }
        public static List<User> SelectCustomer(User user, string strSearch)
        {
            List<User> userList = CDAUser.SelectCustomer(user, strSearch);
            foreach (User _user in userList)
            {
                Crypto crypto = new Crypto();
                _user.Password = crypto.Decrypt(userList.First().Password);
            }
            return userList;
        }

    }
}
